/* lfsystest.c - lfsystest.c */

#include <xinu.h>
#include <stdio.h>
#include <string.h>

/*------------------------------------------------------------------------
 * lfsystest - test local file system
 *------------------------------------------------------------------------
 */
syscall lfsystest(void)
{
	//did32 fd1, fd2;
	did32 fd1;
	char input[100000], output[100000];
	int i;
	struct lflcblk *lfptr;
	struct	ldentry	*dirptr;	/* Ptr to file's entry in the dir	*/
	
	for(i=0;i<100000;i++) {
		input[i] = (char)(i%256);
		output[i] = ' ';
	}
	
	open(LF_DISK_DEV, "1000", NULL);
	control(LFILESYS, LFS_CTL_FORMAT, 100, 512000);
	lfsckfmt(LF_DISK_DEV);
	
	fd1 = open(LFILESYS, "myfile1", "rw");	
	lfptr = &lfltab[devtab[fd1].dvminor];
	dirptr = lfptr->lfdirptr;
	kprintf("File size = %d\r\n", dirptr->ld_size);
	write(fd1, input, 100000);
	kprintf("File size = %d\r\n", dirptr->ld_size);
	control(LFILESYS, LFS_CTL_TRUNC, (int32)lfptr->lfname, 0);
	kprintf("\r\nAfter LFS_CTL_TRUNC:\r\n");
	kprintf("File size = %d\r\n", dirptr->ld_size);
	
	kprintf("\r\nIntially:\r\n");
	kprintf("ID of current index block = %d\r\n", lfptr->lfinum);
	kprintf("ID of current data block = %d\r\n", lfptr->lfdnum);
	kprintf("Byte position of next byte = %d\r\n", lfptr->lfpos);

	write(fd1, input, 100000);
	kprintf("\r\nAfter write(fd1, input, 100000):\r\n");
	kprintf("ID of current index block = %d\r\n", lfptr->lfinum);
	kprintf("ID of current data block = %d\r\n", lfptr->lfdnum);
	kprintf("Byte position of next byte = %d\r\n", lfptr->lfpos);
	
	seek(fd1, 0);
	kprintf("\r\nAfter seek(fd1, 0):\r\n");
	kprintf("Byte position of next byte = %d\r\n", lfptr->lfpos);
	
	read(fd1, output, 100000);
	kprintf("\r\nAfter read(fd1, output, 100000):\r\n");
	kprintf("ID of current index block = %d\r\n", lfptr->lfinum);
	kprintf("ID of current data block = %d\r\n", lfptr->lfdnum);
	kprintf("Byte position of next byte = %d\r\n", lfptr->lfpos);
	
	for(i=0;i<100000;i++) {
		if (input[i] != output[i])
			printf("Wrong");
			return SYSERR;
	}
	
	//fd2 = open(LFILESYS, "myfile1", "rw");
	
	return OK;
}
